using System;
using System.Collections.Generic;
using System.Text;
using Csla;
using Active.Core.DB;
using Csla.Data;

namespace Active.Core
{
    [Serializable()]
    public class Regions :
      BusinessListBase<Regions, Region>
    {
        #region Business Methonds

        protected override object AddNewCore()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException(
                  "User not authorized to add an Region");
            Region r = Region.NewRegion();
            Add(r);
            return r;
        }

        public Region GetRegionById(int regionId)
        {
            foreach (Region item in this)
            {
                if (item.Id == regionId)
                    return item;
            }
            return null;
        }

        public void Remove(int regionId)
        {
            if (!CanDeleteObject())
                throw new System.Security.SecurityException(
                  "User not authorized to remove an Region");
            foreach (Region item in this)
            {
                if (item.Id == regionId)
                {
                    Remove(item);
                    break;
                }
            }
        }

        #endregion

        #region Authorization Rules

        /// <summary>
        /// Determines whether this instance [can add object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can add object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanAddObject()
        {
            return Utils.IsMediaManager;
        }

        /// <summary>
        /// Determines whether this instance [can get object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can get object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanGetObject()
        {
            return true;
        }

        /// <summary>
        /// Determines whether this instance [can edit object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can edit object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanEditObject()
        {
            return Utils.IsMediaManager;
        }

        /// <summary>
        /// Determines whether this instance [can delete object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can delete object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanDeleteObject()
        {
            return Utils.IsMediaManager;
        }

        #endregion

        #region Factory Methods

        //internal static Regions NewRegions()
        //{
        //    return new Regions();
        //}

        private static Regions _regions = null;

        /// <summary>
        /// Gets the regions.
        /// </summary>
        /// <returns></returns>
        public static Regions GetRegions()
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException(
                  "User not authorized to get Regions");
            if (_regions == null)
            {
                _regions = DataPortal.Fetch<Regions>(new Criteria());
            }
            return _regions;
        }

        private Regions()
        { /* Require use of factory methods */ }

        /// <summary>
        /// Saves the object to the database.
        /// </summary>
        /// <returns>
        /// A new object containing the saved values.
        /// </returns>
        /// <remarks>
        /// 	<para>
        /// Calling this method starts the save operation, causing the all child
        /// objects to be inserted, updated or deleted within the database based on the
        /// each object's current state.
        /// </para><para>
        /// All this is contingent on <see cref="IsDirty"/>. If
        /// this value is <see langword="false"/>, no data operation occurs.
        /// It is also contingent on <see cref="IsValid"/>. If this value is
        /// <see langword="false"/> an exception will be thrown to
        /// indicate that the UI attempted to save an invalid object.
        /// </para><para>
        /// It is important to note that this method returns a new version of the
        /// business collection that contains any data updated during the save operation.
        /// You MUST update all object references to use this new version of the
        /// business collection in order to have access to the correct object data.
        /// </para><para>
        /// You can override this method to add your own custom behaviors to the save
        /// operation. For instance, you may add some security checks to make sure
        /// the user can save the object. If all security checks pass, you would then
        /// invoke the base Save method via <c>MyBase.Save()</c>.
        /// </para>
        /// </remarks>
        public override Regions Save()
        {
            if (!CanEditObject())
                throw new System.Security.SecurityException(
                  "User not authorized to update an Region");
            _regions = base.Save();
            return _regions;
        }

        #endregion

        #region Data Access

        [Serializable()]
        private class Criteria
        {
            public Criteria() { }
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            RaiseListChangedEvents = false;
            NBearLite.SelectSqlSection select =
                DataService.DBService.Select(Tables.Regions);

            using (SafeDataReader dr = new SafeDataReader(select.ToDataReader()))
            {
                while (dr.Read())
                {
                    this.Add(Region.GetRegion(dr));
                }
            }
            RaiseListChangedEvents = true;
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            RaiseListChangedEvents = false;
            foreach (Region item in DeletedList)
                item.DeleteSelf();
            DeletedList.Clear();

            foreach (Region item in this)
            {
                if (item.IsNew)
                {
                    item.Insert();
                }
                else
                    item.Update();
            }
            RaiseListChangedEvents = true;
        }

        #endregion
    }
}
